<?
session_start();
require_once 'php/connect.php';

$uName = $_SESSION['userName'];
$uId = $_SESSION['userId'];
$start = mysql_real_escape_string($_POST['start']);

$mode = mysql_real_escape_string($_POST['mode']);

if(!isset($_POST['start']) || $_POST['start'] == "" || $_POST['start'] < 0){
	$start = 0;
}

$output = " <script>				
				$(document).ready(function(){
					$('.load-message').click(function(e) {	
						var clicked = jQuery(e.target);
						var id = clicked.	attr('id');
						deleteSpec(id);
					});
					$('.show-user').click(function(e) {	
						var clicked = jQuery(e.target);
						var id = clicked.attr('id');
						getUser(id);
					});
					$('.show-more').click(function(e) {	
							loadMessageList(".($start+6).")
					});
					$('.show-less').click(function(e) {	
							loadMessageList(".($start-6).")
					});
				});
			</script>
			<table id='message_listbox' class='content-subtitle-table' style='width: 733; '>";
				
	
$abfrage = "SELECT 
		msg.messageId
		,msg.fromId
	    ,msg.toId
		,msg.title
		,msg.message
		,fromUsers.userName as fromUser
		,toUsers.userName as toUser
		,msg.udate
		,mem.memoryId
		FROM (((
		ffxivc_messages as msg 
		LEFT JOIN 
		ffxivc_users as fromUsers
		ON msg.fromId = fromUsers.userId) 
		LEFT JOIN
		ffxivc_users as toUsers
		ON msg.toId = toUsers.userId) 
		LEFT JOIN
		ffxivc_memory as mem
		ON msg.messageId = mem.parentId
		AND mem.type = 'MSG'
		AND mem.userId = \"".$_SESSION['userId']."\") 
		WHERE (msg.toId = \"".$uId."\" OR msg.fromId = \"".$uId."\")
		AND msg.parentId = '0' 
		ORDER BY msg.udate DESC
		LIMIT ".$start.", 7
		";
			
			
$max = 6;
$res = mysql_query($abfrage);
$num = mysql_num_rows($res);	
$i = 1;
$rest = $max - $num;

if($res){

	$prev = "";
	if ($start > 0){
		$prev = "arrow-up";
	}
		$output .= "
		<tr class='tr-table' style='text-align:center;'>
			<td class='td-table vertical-middle' colspan='4' style='padding-bottom: 5px;'><div style='display:inline-block;' class='show-less ".$prev." center cur-point'/></td>
		</tr>";


	while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) {
		$newMsgClass = '';
		if($row['memoryId']==''){
			$newMsgClass = 'color-white';
		}
		$output .= "
			<tr id='".$row['messageId']."' class='message-row tr-table ";
		if($i != $max && $i != $num){
			$output .= " border-bottom ";
		}
		$output .= "' name='".$row['messageId']."'>
				<td  class='td-table vertical-middle pad-5 ' style='width: 28px;'><div class='change-color-".$row['messageId']." normal-text ".$newMsgClass."'>";
		if($uId != $row['fromId']){
			$output .= "From: </div>
						</td>
						<td  class='td-table vertical-middle pad-5 cur-point' style='width:150px; overflow:hidden;white-space: nowrap;'><div id='".$row['fromUser']."' class='show-user normal-text  blue-text '>".$row['fromUser']."</div></td>";
		}else{
			$output .= "To:  </div>
						</td>
						<td class='td-table vertical-middle pad-5 cur-point' style='width:150px; overflow:hidden;white-space: nowrap;'><div id='".$row['toUser']."' class=' show-user normal-text blue-text '>".$row['toUser']."</div></td>";
		}
		$output .= "
				<td class='td-table vertical-middle pad-10 pad-lr-20 cur-point' ><div class='change-color-".$row['messageId']." normal-text  ".$newMsgClass."' style='overflow: hidden;width: 287px;white-space: nowrap;'>".$row['title']."</div></td>
				<td class='td-table vertical-middle pad-10 pad-lr-20 cur-point' style='text-align: right;width: 125px;'><div class='change-color-".$row['messageId']." normal-text font-10 ".$newMsgClass."'>".$row['udate']."</div></td>
			</tr>";
		if($i==1 && $start == 0){
			$output .= "<script>loadMessage(".$row['messageId'].",5)</script>";
		}
		if($i == $max){
			break;
		}	
		
		$i++;
		
	}
	 $nex = "";
	if ($num > $max){
	 $nex = "arrow-down";
	}
		$output .= "
		<tr class='tr-table' style='text-align:center;'>
			<td class='td-table vertical-middle' colspan='4' style='padding-top: 5px;'><div style='display:inline-block;' class='show-more ".$nex." cemter cur-point'/></td>
		</tr>";
				


	mysql_free_result($res);
			
		$output .= "	</table>
						<script type='text/javascript'><!--

						$('.message-row').click(function(e) {
							var clicked = $(e.target);
							var tr = $(this).closest('tr');
							var specId = tr.attr('name');
							if (clicked.attr('name') != 'no'){
								loadMessage(specId, 5);
							}
						});
						
					//-->
				</script>";
}
echo  $output;

?>